package com.lingjtx.common.thread.pooling;

import com.lingjtx.common.core.handle.ObjectPool;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * 对象池注册中心
 */
@Component
public class ObjectPoolRegistry {

    private final Map<String, ObjectPool<?>> poolMap = new ConcurrentHashMap<>();

    public void register(String name, ObjectPoolTemplate<?> pool) {
        poolMap.put(name, pool);
    }

    @SuppressWarnings("unchecked")
    public <T> ObjectPool<T> get(String name) {
        return (ObjectPoolTemplate<T>) poolMap.get(name);
    }

    public Set<String> getPoolNames() {
        return Collections.unmodifiableSet(poolMap.keySet());
    }

    public Map<String, Map<String, Object>> getResourceMonitorMap() {
        return poolMap.entrySet().stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey, // 使用 poolMap 中的 key 作为新的 Map 的键
                        entry -> entry.getValue().getResourceMonitorMap()
                ));
    }
}
